from django.db import models

# Create your models here.
SWAP_MAKERS = [
    ('玖行', '玖行'),
    ('融青', '融青'),
    ('智锂', '智锂'),
    ('双杰', '双杰'),
    ('三一', '三一'),
]
SOFTWARE_VERSIONS = [
    ('2', '玖行站控'),
    ('7', '自研站控'),
]
GROUP_IDS = [
    ('cidLSNmQcBGApGfJ2fs7s/ZwA==', '事业一部'),
    ('cid3tEqV2PxMArhN43tS4K5+w==', '事业二部'),
    ('cidROVaJHQAgYcipeqZGaXUPQ==', '事业三部'),
    ('cid1a02dNB/KB3epiHv8xR9GQ==', '事业四部'),
    ('cidq+M7ADQGkt4r1g99QbJ/og==', '事业五部'),
    ('cidRU9GZuy+Kun62rYjtTxV6w==', '事业六部'),
]
BUSINESS_UNITS = [
    ('事业一部', '事业一部'),
    ('事业二部', '事业二部'),
    ('事业三部', '事业三部'),
    ('事业四部', '事业四部'),
    ('事业五部', '事业五部'),
    ('事业六部', '事业六部'),
]


class Algorithm(models.Model):
    algorithm_name = models.CharField(primary_key=True, max_length=20, verbose_name="智能巡检算法名称")
    algorithm_topic = models.CharField(max_length=40, verbose_name="巡检请求主题")
    inspect_result_topic = models.CharField(max_length=40, verbose_name="巡检结果主题")
    intermediate_result_topic = models.CharField(max_length=40, verbose_name="中间结果主题")
    swap_maker = models.CharField(max_length=20, verbose_name="适配换电站厂家")
    alarm_name = models.CharField(max_length=30, verbose_name="告警名称")
    inspect_type = models.CharField(max_length=20, verbose_name="巡检类型")

    class Meta:
        db_table = "ai_inspect_algorithm"
        verbose_name = "智能巡检算法"
        managed = False


class AlgorithmMetric(models.Model):
    algorithm = models.OneToOneField(
        Algorithm,
        primary_key=True,
        on_delete=models.CASCADE,
        related_name='algorithm',
        verbose_name='所属智能巡检算法',
        db_column='algorithm_name',
    )
    station_request = models.IntegerField(verbose_name="请求换电站的数量")
    channel_request = models.IntegerField(verbose_name="请求摄像头的数量")
    station_result = models.IntegerField(verbose_name="返回换电站的数量")
    channel_result = models.IntegerField(verbose_name="返回监控通道的数量")
    unverified = models.IntegerField(verbose_name="未复核的数量")
    no_target = models.IntegerField(verbose_name="检出结果为无目标的数量")
    normal = models.IntegerField(verbose_name="检出结果为正常的数量")
    broken = models.IntegerField(verbose_name="检出结果为异常的数量")
    tp_intermediate = models.IntegerField(verbose_name="中间结果TP")
    fp_intermediate = models.IntegerField(verbose_name="中间结果FP")
    tn_intermediate = models.IntegerField(verbose_name="中间结果TN")
    fn_intermediate = models.IntegerField(verbose_name="中间结果FN")
    acc_intermediate = models.FloatField(verbose_name="中间结果准确率")
    precision_intermediate = models.FloatField(verbose_name="中间结果查准率")
    recall_intermediate = models.FloatField(verbose_name="中间结果召回率")
    tp_final = models.IntegerField(verbose_name="最终结果TP")
    fp_final = models.IntegerField(verbose_name="最终结果FP")
    tn_final = models.IntegerField(verbose_name="最终结果TN")
    fn_final = models.IntegerField(verbose_name="最终结果FN")
    acc_final = models.FloatField(verbose_name="最终结果准确率")
    precision_final = models.FloatField(verbose_name="最终结果查准率")
    recall_final = models.FloatField(verbose_name="最终结果召回率")
    timestamp = models.DateTimeField(verbose_name="指标评价时间")

    class Meta:
        db_table = "ai_algorithm_metrics"
        verbose_name = "智能巡检算法指标"
        # managed = False


class StationType(models.Model):
    swap_maker = models.CharField(max_length=10, choices=SWAP_MAKERS, verbose_name="换电站厂商")
    station_model = models.CharField(primary_key=True, max_length=20, verbose_name="换电站型号")

    class Meta:
        db_table = "ai_SWAP_STATION_TYPE"
        verbose_name = "换电站类型"
        managed = False


class RobotType(models.Model):
    swap_maker = models.CharField(max_length=10, choices=SWAP_MAKERS, verbose_name="换电站厂商")
    robot_type = models.CharField(primary_key=True, max_length=20, verbose_name="换电机器人型号")

    class Meta:
        db_table = "ai_SWAP_ROBOT_TYPE"
        verbose_name = "换电机器人型号"
        managed = False


class Station(models.Model):
    station_id = models.CharField(max_length=10, primary_key=True, unique=True, db_column='stationNo')
    station_name = models.CharField(max_length=40, db_column='stationName')
    business_unit = models.CharField(choices=BUSINESS_UNITS, max_length=10, db_column='businessUnit')
    group_id = models.CharField(max_length=40, choices=GROUP_IDS, db_column='groupID')
    station_maker = models.CharField(choices=SWAP_MAKERS, default='玖行', max_length=10, db_column='stationMaker')
    station_type = models.ForeignKey(
        StationType,
        on_delete=models.CASCADE,
        related_name='station',
        verbose_name='所属换电站型号',
        db_column='stationType',
        default="四代站",
    )
    software_version = models.CharField(choices=SOFTWARE_VERSIONS, default='7', max_length=10,
                                        db_column='softwareVersion')
    robot_type = models.ForeignKey(
        RobotType,
        on_delete=models.CASCADE,
        related_name="station",
        verbose_name='所属换电机器人型号',
        db_column="robotType",
        default="玖行站力豪"
    )

    class Meta:
        db_table = "ai_swap_station"
        verbose_name = "换电站列表"
        managed = False


class AiBox(models.Model):
    AIBOX_TYPE = [
        ('MiniBox-H8', '一代'),
        ('TD-N7116', '二代'),
    ]
    station_id = models.OneToOneField(
        Station,
        on_delete=models.CASCADE,
        related_name='aibox',
        primary_key=True,
        db_column='stationNo')
    aibox_type = models.CharField(max_length=20, choices=AIBOX_TYPE, db_column='aiboxType')
    aibox_sn = models.CharField(max_length=20, unique=True, db_column='aiboxSn')
    aibox_url = models.CharField(max_length=50, unique=True, db_column='aiboxURL')
    ai_inspect = models.BooleanField(default=True, db_column='aiInspect')
    monitor_channel = models.CharField(max_length=10, null=True, db_column='monitorChannel')
    online_state = models.BooleanField(verbose_name="在线/离线", null=True)
    last_online_time = models.DateTimeField(verbose_name="最后在线时间", null=True)

    class Meta:
        db_table = "ai_aibox"
        verbose_name = "视频推理机"
        managed = False


class Channel(models.Model):
    channel_id = models.AutoField(primary_key=True)
    aibox_sn = models.ForeignKey(
        AiBox,
        on_delete=models.CASCADE,
        to_field='aibox_sn',
        db_column='aiboxSn',
        max_length=20,
        related_name='channel',
        verbose_name='所属视频推理机'
    )
    channel_number = models.IntegerField(db_column='channelNo')
    channel_name = models.CharField(max_length=20, db_column='channelName')
    channel_ip = models.CharField(max_length=40, db_column='channelIP')
    channel_url = models.CharField(max_length=100, db_column='channelURL')
    channel_algorithm = models.CharField(max_length=20, null=True, db_column='channelAlgorithm')

    class Meta:
        db_table = "ai_monitor_camera_channel"
        verbose_name = "监控摄像头"
        managed = False


class RobotStep(models.Model):
    step_id = models.AutoField(primary_key=True)
    step_code = models.IntegerField()
    step_description = models.CharField(max_length=50)
    robot_type = models.ForeignKey(
        RobotType,
        on_delete=models.CASCADE,
        related_name='robot_step',
        verbose_name='所属换电机器人型号',
        db_column="robot_type"
    )

    class Meta:
        db_table = "ai_swap_robot_step"
        verbose_name = "换电机器人步骤"
        managed = False


class StepRule(models.Model):
    rule_id = models.AutoField(primary_key=True)
    algorithm = models.ForeignKey(
        Algorithm,
        on_delete=models.CASCADE,
        related_name='step_rule',
        verbose_name='所属巡检算法',
        db_column="algorithm",
    )
    station = models.ForeignKey(
        Station,
        on_delete=models.CASCADE,
        related_name='step_rule',
        verbose_name='所属换电站',
        db_column="station_id",
    )
    channel = models.ForeignKey(
        Channel,
        on_delete=models.CASCADE,
        related_name='step_rule',
        verbose_name='所属监控摄像头'
    )
    activate = models.BooleanField(default=True)
    step = models.ForeignKey(
        RobotStep,
        to_field='step_id',
        on_delete=models.CASCADE,
        related_name='step_rule',
        verbose_name='所属换电步骤'
    )

    class Meta:
        db_table = "ai_inspect_robot_step_rule"
        verbose_name = "基于换电机器人步骤的智能巡检"
        managed = False


class PeriodRule(models.Model):
    PERIOD = [
        ('每小时', '每小时'),
        ('每天', '每天')
    ]
    rule_id = models.AutoField(primary_key=True)
    algorithm = models.ForeignKey(
        Algorithm,
        on_delete=models.CASCADE,
        related_name='period_rule',
        verbose_name='所属巡检算法',
        db_column="algorithm",
    )
    station = models.ForeignKey(
        Station,
        on_delete=models.CASCADE,
        related_name='period_rule',
        verbose_name='所属换电站'
    )
    channel = models.ForeignKey(
        Channel,
        on_delete=models.CASCADE,
        related_name='period_rule',
        verbose_name='所属监控摄像头'
    )
    activate = models.BooleanField(default=True)
    period = models.CharField(max_length=10, choices=PERIOD)

    class Meta:
        db_table = "ai_inspect_period_rule"
        verbose_name = "周期性智能巡检"
        managed = False


class SwapPhoto(models.Model):
    photo_url = models.CharField(primary_key=True, unique=True, max_length=200, verbose_name="照片URL")
    station = models.ForeignKey(
        Station,
        on_delete=models.CASCADE,
        related_name='swap_photo',
        verbose_name="换电站编号"
    )
    station_name = models.CharField(max_length=30, verbose_name="换电站名称")
    photo_time = models.DateTimeField(verbose_name="抓拍时间")
    channel_name = models.CharField(max_length=10, verbose_name="监控摄像头名称")
    order_sn = models.CharField(max_length=30, null=True)

    class Meta:
        db_table = "ai_swap_station_photo"
        verbose_name = "换电站抓拍图片"
        managed = False


class SwapRecord(models.Model):
    newCabinetNo = models.IntegerField(verbose_name="满电电池仓位号")
    newBatSoc = models.FloatField(verbose_name="满电电池SOC")
    stationNo = models.CharField(max_length=10, verbose_name="换电站编号")
    plateNo = models.CharField(max_length=20, verbose_name="车牌号码", null=True)
    orderSn = models.CharField(primary_key=True, max_length=30, verbose_name="换电订单编号")
    newBatID = models.CharField(max_length=30, verbose_name="满电电池编号")
    swapType = models.IntegerField(verbose_name="换电/换仓")
    oldCabinetNo = models.IntegerField(verbose_name="亏电电池仓位号")
    swapStartType = models.IntegerField(verbose_name="换电启动方式")
    timeStamp = models.DateTimeField(verbose_name="数据上报时间")
    rfidCode = models.CharField(max_length=50, verbose_name="车辆RFID", null=True)
    vin = models.CharField(max_length=50, verbose_name="车辆VIN码")
    startTime = models.DateTimeField(verbose_name="换电开始时间")
    stopTime = models.DateTimeField(verbose_name="换电完成时间")
    oldBatID = models.CharField(max_length=30, verbose_name="亏电电池编号")
    oldBatSoc = models.FloatField(verbose_name="亏电电池SOC")

    class Meta:
        db_table = "ai_power_swap_service_record"
        verbose_name = "换电服务记录"
        managed = False


class InspectRequest(models.Model):
    # request_id = models.AutoField(primary_key=True, db_column="request_id")
    request_name = models.CharField(max_length=200, primary_key=True, verbose_name="巡检请求名称")
    inspect_name = models.CharField(max_length=40, verbose_name="巡检算法名称")
    station_id = models.CharField(max_length=10, verbose_name="换电站编号")
    station_name = models.CharField(max_length=30, verbose_name="换电站名称")
    device_sn = models.CharField(max_length=30, verbose_name="AIBOX序列号")
    channel_number = models.IntegerField(verbose_name="监控通道编号")
    channel_name = models.CharField(max_length=10, verbose_name="监控通道名称")
    timestamp = models.IntegerField(verbose_name="巡检请求时间")
    photo_url = models.CharField(max_length=200, verbose_name="抓拍照片名称")
    verification = models.CharField(max_length=10, verbose_name="人工复核结果", null=True)

    class Meta:
        db_table = "ai_inspect_request"
        verbose_name = "智能巡检请求"
        managed = False


class InspectResult(models.Model):
    result_id = models.AutoField(primary_key=True)
    request_name = models.CharField(max_length=200, db_column="request_name", verbose_name="巡检请求名称")
    inspect_name = models.CharField(max_length=40, verbose_name="巡检算法名称")
    result_type = models.CharField(max_length=20, verbose_name="最终结果/中间结果")
    code = models.IntegerField()
    msg = models.CharField(max_length=100)
    server_name = models.CharField(max_length=20)
    server_url = models.CharField(max_length=100)
    timestamp = models.IntegerField()
    channel_name = models.CharField(max_length=20, verbose_name="监控通道名称")
    channel_number = models.IntegerField(verbose_name="监控通道号码")
    photo_url = models.CharField(max_length=200, verbose_name="抓拍图片地址")
    station_id = models.CharField(max_length=10, verbose_name="换电站编号")
    station_name = models.CharField(max_length=30, verbose_name="换电站名称")
    detect_picture = models.CharField(max_length=100, verbose_name="检测图片名称")
    event_type = models.CharField(max_length=200, verbose_name="检出事件类型")
    target_type = models.CharField(max_length=20, verbose_name="检出目标类型")
    target_id = models.IntegerField(verbose_name="检出目标序号")
    quality_score = models.FloatField(verbose_name="置信度")
    height = models.IntegerField(verbose_name="目标高度")
    width = models.IntegerField(verbose_name="目标宽度")
    x = models.IntegerField(verbose_name="目标横坐标")
    y = models.IntegerField(verbose_name="目标纵坐标")
    verification = models.CharField(max_length=10, verbose_name="人工复复核结果", null=True)

    class Meta:
        db_table = "ai_inspect_result"
        verbose_name = "智能巡检结果"
        managed = False


class CaptureRequest(models.Model):
    capture_id = models.AutoField(primary_key=True)
    station_id = models.CharField(max_length=10, verbose_name="换电站编号")
    aibox_sn = models.CharField(max_length=30, verbose_name="aibox序列号")
    station_name = models.CharField(max_length=30, verbose_name="换电站名称")
    channel_number = models.IntegerField(verbose_name="监控通道号码")
    channel_name = models.CharField(max_length=20, verbose_name="监控通道名称")
    timestamp = models.IntegerField(verbose_name="抓拍时间")
    step_code = models.IntegerField(null=True, verbose_name="换电步骤码")
    step_description = models.CharField(max_length=100, null=True, verbose_name="换电步骤名称")
    order_sn = models.CharField(max_length=50, null=True, verbose_name="换电订单编号")

    class Meta:
        db_table = "ai_photo_capture_request"
        verbose_name = "抓拍照片请求"
        managed = False


class StationBattery(models.Model):
    station_id = models.CharField(max_length=10, primary_key=True)
    station_name = models.CharField(max_length=50)
    station_maker = models.CharField(max_length=10, choices=SWAP_MAKERS)
    station_model = models.CharField(max_length=10, db_column="station_model")
    product_id = models.CharField(max_length=10, choices=SOFTWARE_VERSIONS, db_column="product_id")
    station_type = models.CharField(max_length=20)
    business_unit = models.CharField(max_length=10, choices=BUSINESS_UNITS)
    charger_count = models.IntegerField()
    battery_count = models.IntegerField()
    battery_count_validate = models.IntegerField(null=True)
    dw_time = models.DateTimeField(null=True)
    validate_time = models.DateTimeField(null=True)

    class Meta:
        db_table = "ai_swap_station_battery_count"
        verbose_name = "换电站仓电池盘点"
        managed = False
